package scenarios.gui.actions;

import dht.identifier.identifier.Identifier;
import dht.identifier.identifier.Identifiers;
import dht.node.DHTDataRow;
import dht.node.DHTNodeModel;
import dht.node.DHTNodeRow;
import scenarios.gui.NetDataTableModel;
import scenarios.net.NetDataRow;
import scenarios.net.TestNode;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by IntelliJ IDEA.
 * User: gvennet
 * Date: 21.02.2010
 * Time: 15:46:02
 */
public class PutValues extends AbstractAction
{
	ArrayList<TestNode> nodes;
	ScheduledThreadPoolExecutor scheduledExecutor;
	NetDataTableModel localDataTableModel;
	ArrayList<NetDataRow> NetDataRows = new ArrayList<NetDataRow>();
	Map<Identifier, Identifier> correctDistribution = new HashMap<Identifier, Identifier>();
	private DHTNodeModel allNodeModel = new DHTNodeModel(1000, null, null);

	public PutValues(ArrayList<TestNode> nodes, ScheduledThreadPoolExecutor scheduledExecutor, NetDataTableModel localDataTableModel)
	{
		super("Добавить данные");
		this.nodes = nodes;
		this.scheduledExecutor = scheduledExecutor;
		this.localDataTableModel = localDataTableModel;
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		JFileChooser fileChooser = new JFileChooser("/Users/gvennet/developing/java/IdeaProjects/dht-net/src/main/java/scenarios");
		fileChooser.showDialog(null, "OK");
		BufferedReader fileReader = null;

		for (TestNode node : nodes)
		{
			allNodeModel.put(new DHTNodeRow(node.getId(), null, node.getPort(), 0));
			node.removingOlds();
		}
		try
		{
			fileReader = new BufferedReader(new FileReader(fileChooser.getSelectedFile()));

		}
		catch (FileNotFoundException e1)
		{
			e1.printStackTrace();
		}

		String str;
		try
		{
			assert fileReader != null;
			str = fileReader.readLine();

			while (str != null)
			{

				String[] parts = str.replaceAll("[.,-:;!?]", " ").split(" ");
				for (String part : parts)
				{
					if (part.length() > 3)
					{
						Identifier partId = Identifiers.createIdentifier(part, 20);
						correctDistribution.put(partId, allNodeModel.get(partId).get(0).getId());
					}
				}
				nodes.get(0).putValue(str);

				str = fileReader.readLine();
			}
		}
		catch (IOException e1)
		{
			e1.printStackTrace();
		}

		scheduledExecutor.scheduleAtFixedRate(new UpdateDataTable(), 0, 1, TimeUnit.SECONDS);
	}

	private class UpdateDataTable implements Runnable
	{
		UpdateDataTable()
		{

		}

		@Override
		public void run()
		{
			NetDataRows.clear();

			int count = 0;

			for (TestNode node : nodes)
			{
				for (DHTDataRow dataRow : node.getDataModel().getData())
				{
					NetDataRows.add(new NetDataRow(dataRow.getValue(), dataRow.getId(), node.getId()));
					if (correctDistribution.get(dataRow.getId()).compareTo(node.getId()) == 0)
					{
						count++;
					}
				}
			}

			System.out.println("Tablesize: " + NetDataRows.size() + " correctLocation: " + count);
			localDataTableModel.newData(NetDataRows);
		}
	}
}
